package com;


import com.example.grpc.HelloRequest;
import com.example.grpc.HelloResponse;
import com.example.grpc.HelloServiceGrpc;
import com.interceptor.HeaderClientInterceptor;
import io.grpc.ManagedChannel;
import io.grpc.ManagedChannelBuilder;

import java.util.concurrent.TimeUnit;

/**
 * 同步阻塞式RPC调用
 */
public class BlockGrpcClient {

    public static void main(String[] args) throws InterruptedException {

        // 客户端连接通道
        ManagedChannel channel = ManagedChannelBuilder.forAddress("localhost", 8081)
                .usePlaintext(true)
                .build();

        HelloServiceGrpc.HelloServiceBlockingStub stub = HelloServiceGrpc.newBlockingStub(channel);


        HelloRequest helloRequest = HelloRequest.newBuilder()
                .setName("Tom")
                .setAge(18)
                .addHobbies("read")
                .putTags("boy", "money")
                .build();

        HelloResponse helloResponse = stub.withInterceptors(new HeaderClientInterceptor()).hello(helloRequest);
        System.out.println(helloResponse);

        channel.shutdown().awaitTermination(10, TimeUnit.SECONDS);
    }
}